﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.ZNH.ZMCA.UI.Controls;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCA.UI.Utils;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCA.UI.PopWindows;

namespace Baosight.ZNH.ZMCA.UI
{
    public partial class ZMCA44 : EFViewCmes
    {
        private const int fontSizeNumber = 14;
        /// <summary>
        /// 生产重点关注
        /// 供00S00Portal.xmal页面调用
        /// </summary>
        public ZMCA44()
        {
            InitializeComponent();
            this.Loaded+=new RoutedEventHandler(ZMCA44_Loaded);
        }

        private void ZMCA44_Loaded(object sender, RoutedEventArgs e)
        {//后台SQL对待显示的记录数进行限制
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA42");
            eiInfo.Set("method", "query");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, onInitLoadCompleted);
        }

        private void onInitLoadCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("数据加载失败！");
                return;
            }
            else
            {
                //btnMoreIndications.FontSize = fontSizeNumber;
                //显示前5条记录
                //Grid grid = new Grid();
                //grid.RowDefinitions
                //this.mainAttention.SetValue();

                EIBlock block = e.EIInfo.GetBlock("result");
                if (block != null && block.GetRowCount() >= 5)
                {
                    for (int i = block.GetRows().Count; i > 4; i--)
                    {
                        EIRow row = block.GetRow(i);
                        block.GetRows().Remove(row);
                    }
                }

                DateTime nowTime = new DateTime();
                nowTime = System.DateTime.Now;
                string time = nowTime.ToString("yyyy-MM-dd");

                for (int i = 1; i <= block.GetRows().Count; i++)
                {
                    EIRow row = block.GetRow(i-1);

                    string desc = row["fdProblemdesc"].ToString();
                    string date = row["fdTime"].ToString();

                    StackPanel sp = new StackPanel();
                    sp.Name = "mainAttention" + i;
                    sp.Orientation = Orientation.Horizontal;
                    sp.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    sp.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    sp.SetValue(Grid.RowProperty, i );
                    sp.SetValue(Grid.ColumnProperty, 0);
                    
                    HyperlinkButton hb = new HyperlinkButton();
                    hb.Name = "btnAttention" + i;
                    hb.Background = new SolidColorBrush(Colors.Transparent);
                    hb.Foreground = new SolidColorBrush(Colors.Black);
                    hb.FontSize = fontSizeNumber;
                    hb.Margin = new Thickness(15, 0, 0, 0);
                    hb.DataContext = row;
                    hb.Content = InterceptStringConverter.GetAttetionDesc(date, ref desc);
                    hb.Click += new RoutedEventHandler(HyperlinkButton_Click);
                    
                    TextBlock tb = new TextBlock();
                    tb.Name = "tbAttention" + i;
                    tb.Text = "今日新增";
                    tb.Foreground = new SolidColorBrush(Colors.Red);
                    tb.Visibility = System.Windows.Visibility.Collapsed;

                    sp.Children.Add(hb);
                    sp.Children.Add(tb);

                    if (date == time)
                    {
                        tb.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        tb.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    LayoutRoot.Children.Add(sp);
                }
            }
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;

            EIRow row = btn.DataContext as EIRow;
            //string id = (btn.Parent.FindChildByType<TextBlock>().FindName("id") as TextBlock).Text.ToString();
            //string type = (btn.Parent.FindChildByType<TextBlock>().FindName("type") as TextBlock).Text.ToString();
            string id = row["fdKeyId"].ToString();
            string type = row["fdProblemTypeId"].ToString(); ;

            ZMCASpecialCommon.ShowPopwindows(type, id, content_DataChanged, true);   
        }

        void content_DataChanged(object sender, EventArgs e)
        {
            UpdateHomePageData(new EIInfo());
        }

        private void btnMoreIndications_Click(object sender, RoutedEventArgs e)
        {
            IShell shell = this.Container.Resolve<IShell>();
            Dictionary<string, string> paraData = new Dictionary<string, string>();
            shell.LoadView("ZMCA42", paraData);
        }

        ///<summary>
        ///更新主表
        ///</summary>
        ///<param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }
    }

    public class InterceptStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string date = Common.GetStringValue(value.GetType().GetProperty("fdTime").GetValue(value, null)) + " ";
            string desc = Common.GetStringValue(value.GetType().GetProperty("fdProblemdesc").GetValue(value, null));
            return GetAttetionDesc(date, ref desc);
        }

        public static object GetAttetionDesc(string date, ref string desc)
        {
            if (desc != null && desc.ToString().Length > 14)
            {
                desc = desc.ToString().Replace("\r", "");
                return date + " " + desc.ToString().Substring(0, 14) + "...";
            }
            else
            {
                return date + " " + desc;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class IsTodayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime nowTime = new DateTime();
            nowTime = System.DateTime.Now;
            string time = nowTime.ToString("yyyy-MM-dd");

            if (value.ToString() == time)
            {
                return "   "+"今日新增！";
            }
            else
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
